"""
Module: Main
Author: Luo Zhe
C-Date: 2009-6-7
"""
import paths

from control.Common import TrackerControl, OpenFileControl, AddSeedControl, RegionGrowControl,AddRulerControl,ManualSegmentationControl, ExitProgram
from glo import ID_MENU_FILE_OPEN, \
                ID_MENU_FILE_EXIT, \
                ID_MENU_LAYOUT_2X2, \
                ID_MENU_LAYOUT_1P3, \
                ID_MENU_LAYOUT_VCCHANGE, \
                ID_MENU_TRACK_OPENTRACKER, \
                ID_MENU_TRACK_STOP, \
                ID_MENU_HELP_ABOUT, \
                setCurrentApplication, \
                setWindowWidth,    \
                setWindowHeight,   \
                setControlPanelWidth,   \
                setCurrentWindow, \
                getCurrentWindow, \
                setTrackerControl, \
                getTrackerControl
from gui.MainWindow import MainWindow
from gui.Layouts import SetLayoutTo2X2, SetLayoutTo1X3
import sys, os, thread, time

from wxPython.wx import *
from wxPython.lib.splashscreen import SplashScreen

"""get resolution of system"""
width, height = wxDisplaySize()
print width, height


setWindowWidth(width)
setWindowHeight(height)
setControlPanelWidth(width*0.23)

class App(wxApp):
    def OnInit(self):
        # the main window
        setCurrentApplication( self )
        thread.start_new_thread(self.playLoginMusic, ())
        self.splash = SplashScreen(None, bitmapfile='resource/login/Surg_splashscreen.bmp',
                                      duration=8000, callback=self.loadMainWindow)
        self.splash.Show(true)

        return 1

    def loadMainWindow(self):
        self.__Window = MainWindow( None, style = wxDEFAULT_FRAME_STYLE, size = ( width, height ) )
        setCurrentWindow( self.__Window )
        getCurrentWindow().getGeneralFunctionPanel().initGeneralFunctionFrames()
        self.__Window.setDefaultImage()
        self.connectEvents( self.__Window )

        self.__Window.Show( 1 )
        self.splash.Destroy()

    def playLoginMusic (self):
        try:
            wave = wxWave('resource/login/Surg_introsound.wav')
            wave.Play()
        except NotImplementedError, v:
            wxMessageBox(str(v), "Exception Message")
        time.sleep(8)

    def connectEvents( self, window ):
        openfilecont = OpenFileControl( window, window.getViews() )
        EVT_MENU( window, ID_MENU_FILE_OPEN, openfilecont )

        exitCont = ExitProgram( self )
        EVT_MENU( window, ID_MENU_FILE_EXIT, exitCont )
        EVT_CLOSE( window, exitCont )

        EVT_MENU( window, ID_MENU_LAYOUT_2X2, lambda event: SetLayoutTo2X2( window.getViews() ) )
        EVT_MENU( window, ID_MENU_LAYOUT_1P3, lambda event: SetLayoutTo1X3( window.getViews() ) )
        EVT_MENU( window, ID_MENU_LAYOUT_VCCHANGE, getCurrentWindow().changeViewControlLayout )

        tracker_control = TrackerControl()
        setTrackerControl(tracker_control)
        EVT_MENU( window, ID_MENU_TRACK_OPENTRACKER, tracker_control.openTracker )
        EVT_MENU( window, ID_MENU_TRACK_STOP, tracker_control.stopTracking )

        EVT_MENU( window, ID_MENU_HELP_ABOUT, window.onHelpAbout )

        addSeedConl = AddSeedControl( window.getViews() )
        addSeedConl(None)

        region_growing_cont = RegionGrowControl(window.getViews())
        region_growing_cont(None)

        manual_segmentation_cont = ManualSegmentationControl(window.getViews())
        manual_segmentation_cont(None)

        addRulerConl = AddRulerControl(window.getViews())
        addRulerConl(None)


app = App(0)
app.MainLoop()

